<?php
declare(strict_types=1);


namespace App\Game\Mapper;

use App\Game\Api\Service\Platform\GameInterface;
use App\Game\Model\Game;
use App\Game\Model\GamePlatform;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;
use Mine\Helper\Str;

/**
 * 游戏平台Mapper类
 */
class GamePlatformMapper extends AbstractMapper
{
    /**
     * @var GamePlatform
     */
    public $model;

    public function assignModel()
    {
        $this->model = GamePlatform::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        return $query;
    }

    public function gameSync(int $id): bool
    {
        $md = $this->model::query()->find($id);
        if ($md->code) {
            /* @var GameInterface $cs */
            $cs = '\\App\\Game\\Api\\Service\\Platform\\' . Str::studly($md->code) . 'Service';
            return $cs::syncGameList();
        }
        return false;
    }

    public function update(mixed $id, array $data): bool
    {
        if (parent::update($id, $data)) {
            /* @var GameInterface $cs */
            $cs = '\\App\\Game\\Api\\Service\\Platform\\' . Str::studly($data['code']) . 'Service';
            $cs::getConfig(true);
            return true;
        }
        return false;
    }

    public function clear(int $id): bool
    {
        Game::query()->where(['platform' => GamePlatform::query()->where(['id' => $id])->value('code')])->forceDelete();
        return true;
    }

    public function getQuota(string $code, string $symbol): array
    {
        /* @var GameInterface $cs */
        $cs = '\\App\\Game\\Api\\Service\\Platform\\' . Str::studly($code) . 'Service';
        return $cs::getQuota($symbol);
    }
}